import {defineStore} from 'pinia';

export const useUserStore = defineStore('userStore', {
  state: () => {
    const isAuthorized = localStorage.getItem('isAuthorized');
    const username = localStorage.getItem('username');

    return {
      username,
      isAuthorized
    };
  },
  getters: {},
  actions: {
    setUsername(username: string) {
      this.username = username;
      localStorage.setItem('username', username);
    },
    setAuthorized(isAuthorized: string) {
      this.isAuthorized = isAuthorized;

      if (isAuthorized) {
        localStorage.setItem('isAuthorized', isAuthorized);
      } else {
        localStorage.removeItem('isAuthorized');
      }
    }
  }
});
